package jp.co.kayo.android.sample.mediaplayer;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener, OnPreparedListener, OnCompletionListener {
    String MEDIA_PATH = "<MP3 URL>";
    MediaPlayer mPlayer;
    RelayService mRelaySrvice;
    String mPlayingUri;
    Button mBtnDirect;
    Button mBtnRelay;
    File mCacheFile;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mBtnDirect = (Button)findViewById(R.id.btnDirect);
        mBtnRelay = (Button)findViewById(R.id.btnRelay);
        
        mBtnDirect.setOnClickListener(this);
        mBtnRelay.setOnClickListener(this);
        
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            File cacheDir = new File(Environment.getExternalStorageDirectory(), this.getPackageName());
            cacheDir.mkdirs();
            mCacheFile = new File(cacheDir, "temp.mp3");
            if(mCacheFile.exists()){
                mCacheFile.delete();
            }
            mRelaySrvice = new RelayService(mCacheFile);
            mRelaySrvice.start();
        }
        else{
            Toast.makeText(this, "SD Card unmount", Toast.LENGTH_LONG).show();
        }
        
        mPlayer = new MediaPlayer();
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnCompletionListener(this);
    }
    
    

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mRelaySrvice!=null){
            mRelaySrvice.stop();
        }
        mPlayer.release();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btnDirect){
            procDirect();
        }
        else if(v.getId() == R.id.btnRelay){
            procRelay();
        }
        
    }
    
    @Override
    public void onPrepared(MediaPlayer mp) {
        mPlayer.start();
        btnEnable(true);
    }
    
    @Override
    public void onCompletion(MediaPlayer mp) {
        btnEnable(true);
        mPlayingUri = null;
        mPlayer.reset();
        mBtnDirect.setText("PLAY");
        mBtnRelay.setText("PLAY");
    }
    
    private void btnEnable(boolean b){
        mBtnDirect.setEnabled(b);
        mBtnRelay.setEnabled(b);
    }
    
    private void procDirect(){
        try {
            if(mPlayer.isPlaying()){
                mPlayingUri = null;
                mPlayer.stop();
                mPlayer.reset();
                mBtnDirect.setText("PLAY");
            }
            else{
                mPlayingUri = MEDIA_PATH;
                mPlayer.setDataSource(this, Uri.parse(MEDIA_PATH));
                mPlayer.prepareAsync();
                mBtnDirect.setText("STOP");
                btnEnable(false);
            }
        } catch (IllegalArgumentException e) {
            Log.e("procDirect", e);
        } catch (SecurityException e) {
            Log.e("procDirect", e);
        } catch (IllegalStateException e) {
            Log.e("procDirect", e);
        } catch (IOException e) {
            Log.e("procDirect", e);
        }
    }
    
    private void procRelay(){
        try {
            if(mPlayer.isPlaying()){
                mPlayingUri = null;
                mPlayer.stop();
                mPlayer.reset();
                mBtnRelay.setText("PLAY");
            }
            else if(mRelaySrvice!=null){
                if(mCacheFile.exists()){
                    mPlayingUri = mCacheFile.getAbsolutePath();
                    mPlayer.setDataSource(mPlayingUri);
                }
                else{
                    mPlayingUri = "http://127.0.0.1:" + mRelaySrvice.getPort() + "/" + MEDIA_PATH;
                    mPlayer.setDataSource(this, Uri.parse(mPlayingUri));
                }
                mPlayer.prepareAsync();
                mBtnRelay.setText("STOP");
                btnEnable(false);
            }
        } catch (IllegalArgumentException e) {
            Log.e("procDirect", e);
        } catch (SecurityException e) {
            Log.e("procDirect", e);
        } catch (IllegalStateException e) {
            Log.e("procDirect", e);
        } catch (IOException e) {
            Log.e("procDirect", e);
        }
    }




}